package action;

import com.opensymphony.xwork2.ModelDriven;
import dao.BookDAO;
import dao.CategoryDAO;
import model.BookModel;

import java.util.List;


public class BookManagerAction extends BaseAction implements ModelDriven<BookModel> {

    private BookModel model = new BookModel();

    private List<BookModel> booklist;  //书籍集合
    private int categoryId;
    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public List<BookModel> getBooklist() {
        return booklist;
    }

    public void setBooklist(List<BookModel> booklist) {
        this.booklist = booklist;
    }

  
    /**
     * 执行新增书籍操作
     * @return
     */
    public String doadd(){
        BookDAO dao = new BookDAO();
        if(dao.createBook(this.getModel())){
            addActionMessage("新增书籍成功！");
        }else{
            addActionMessage("新增书籍失败！");
        }
         return "result";
    }
    /**
     * 根据传来的书籍标识，查询书籍信息，跳转到书籍编辑页面
     * @return
     */
    public String edit(){
        BookDAO dao = new BookDAO();
        this.model = dao.getBookById(this.getModel().getId());
        CategoryDAO cd = new CategoryDAO();
        this.cateList = cd.findAllCategory();
        return "edit";
    }

    /**
     * 删除书籍信息
     * @return
     */
    public String delete(){
        BookDAO dao = new BookDAO();
        dao.deleteBookById(this.getModel().getId());
        addActionMessage("删除书籍信息成功！");
        return "result";
    }

    /**
     * 执行编辑书籍信息操作
     * @return
     */
    public String doedit(){
        BookDAO dao = new BookDAO();
        if(dao.updateBook(this.getModel())){
            addActionMessage("更新书籍信息成功！");
        }else{
            addActionMessage("更新书籍信息失败！");
        }
        return "result";
    }

    /**
     * 后台管理员书籍管理
     * @return
     */
    public String list(){
        BookDAO dao = new BookDAO();
        this.booklist = dao.findBookByQuery(this.getCategoryId(),this.getModel().getBookName());
        return "list";
    }


    @Override
    public BookModel getModel() {
        return model;
    }

}
